package cn.car.controller;

import cn.car.biz.carAccess.PlateDetectedBiz;
import cn.car.biz.convert.ZsHttpConvert;
import cn.car.domains.info.InternalGIoInInfo;
import cn.car.domains.info.InternalHeartDataInfo;
import cn.car.domains.info.InternalSnapshotInfo;
import cn.car.domains.info.PlateDetectedInternalInfo;
import cn.car.domains.vo.zc.*;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/zsCameraPush")
@Slf4j
public class ZSCameraController {

    @Resource
    private ZsHttpConvert zsHttpConvert;

    @Resource
    private PlateDetectedBiz plateDetectedBiz;


    /**
     * 做识别逻辑 需要返回是否开门
     */
    @PostMapping("/plateDetected")
    public ZSPlateDetectedRes plateDetectedRequest(@RequestBody ZSPlateDetectedRequest request) {

        ZSPlateDetectedRes zsPlateDetectedRes = new ZSPlateDetectedRes();

        //类型转换
        PlateDetectedInternalInfo plateDetectedInternalInfo = zsHttpConvert.parsePlateDetectedRequest(request);

        //参数校验
        checkPlateDetectedInternal(plateDetectedInternalInfo);

        //做识别逻辑
        Boolean result = plateDetectedBiz.receiveCarDetectedRequest(plateDetectedInternalInfo);


        //出参转换
        zsPlateDetectedRes.setResponse_AlarmInfoPlate(zsHttpConvert.parsePlateDetectedResult(result, false));
        return zsPlateDetectedRes;

    }

    private void checkPlateDetectedInternal(PlateDetectedInternalInfo plateDetectedInternalInfo) {


    }


    @PostMapping("/heartbeat")
    public ZSHeartbeatResp receiveHeartbeat(ZSHeartbeatRequest request) {
        ZSHeartbeatResp zsHeartbeatResp = new ZSHeartbeatResp();
        InternalHeartDataInfo internalHeartDataInfo = zsHttpConvert.parseZSHeartbeatRequest(request);
        plateDetectedBiz.receiveHeartbeat(internalHeartDataInfo);
        AlarmInfoPlateResp alarmInfoPlateResp = zsHttpConvert.parsePlateDetectedResult(false, true);
        zsHeartbeatResp.setResponseAlarmInfoPlate(alarmInfoPlateResp);
        return zsHeartbeatResp;
    }

    /**
     * 截图数据推送
     */
    @PostMapping("/snapshotIn")
    public void snapshotIn(@RequestBody ZSSnapshotRequest request) {
        final ZSSnapshotRequest.TriggerImage triggerImage = request.getTriggerImage();
        if (triggerImage == null) {
            return;
        }

        if (!NumberUtil.isNumber(request.getTriggerImage().getImageFileLen())) {
            return;
        }
        InternalSnapshotInfo requestInternal = new InternalSnapshotInfo(request.getIpaddr(), triggerImage.getImageFile(), Integer.valueOf(triggerImage.getImageFileLen()));
        plateDetectedBiz.snapshotStorage(requestInternal);

    }


    /**
     * 端口触发信息推送
     */
    @PostMapping("/gIOIn")
    public ZSGIOInRes gIOIn(@RequestBody ZSGIOInRequest request) {
        final ZSGIOInRes response = new ZSGIOInRes();

        InternalGIoInInfo internalGIoInInfo = zsHttpConvert.parseZSGIOInRequest(request);

        log.info("[ZSCameraController gIOIn] requestInternal:{}", JSONObject.toJSONString(internalGIoInInfo));
        plateDetectedBiz.gIOIn(internalGIoInInfo);
        return response;
    }


}
